package com.buer.storage.service.impl;


import com.buer.storage.dao.WarehouseDao;

import com.buer.storage.dto.*;
import com.buer.storage.entity.Area;
import com.buer.storage.entity.Department;
import com.buer.storage.entity.UserEntity;
import com.buer.storage.entity.Warehouse;
import com.buer.storage.service.WarehouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private WarehouseDao warehouseDao;

    //增加仓库
    @Override
    public int insert(WarehouseDto warehouseDto) {
        return warehouseDao.insert(warehouseDto);
    }

    //删除仓库
    @Override
    public int delete(int warehouseId) {
        return warehouseDao.delete(warehouseId);
    }

    //修改仓库
    @Override
    public int update(Warehouse warehouse) {
        return warehouseDao.update(warehouse);
    }

    //根据id查询仓库
    @Override
    public Warehouse selectById(int warehouseId) {
        return warehouseDao.selectById(warehouseId);
    }

    //查询所有
    @Override
    public List<Warehouse> findAll() {
        return warehouseDao.findAll();
    }

    @Override
    public List<Area> findAllArea(int warehouseId) {
        List<Area> area = warehouseDao.findAllArea(warehouseId);
        return area;
    }

    @Override
    public List<DepartmentDto> findAllDepartment(int warehouseId) {
        List<DepartmentDto> department = warehouseDao.findAllDepartment(warehouseId);
        return department;
    }

    @Override
    public List<DepartmentSelectDto> findDepartById (Integer warehouseId){
        List<DepartmentSelectDto> areaAll = warehouseDao.findAreaByWarehouseId(warehouseId);
        if (areaAll.size() > 0) {
            for (int i = 0; i < areaAll.size(); i++) {
                Department[] departmentAll = warehouseDao.findDepartmentByWarehouseId(areaAll.get(i).getAreaId());
                if (departmentAll.length > 0) {
                    areaAll.get(i).setDepartments(departmentAll);
                }
            }
        }

        return areaAll;
    }

    @Override
    public PageInfo<Warehouse> getEntityList(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<Warehouse> list = warehouseDao.getEntityList(map); // 获取数据
        PageInfo<Warehouse> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }
}
